<template>
  <div class="xtx-dialog" v-show="dialogConfig.show">
    <div class="wrapper">
      <div class="header">
        <h3>{{ dialogConfig.title }}</h3>
        <a
          href="JavaScript:;"
          class="iconfont icon-close-new"
          @click="cancel"
        ></a>
      </div>
      <div class="body">
        <slot></slot>
      </div>
      <div class="footer">
        <button class="btn" style="margin-right: 20px" @click="cancel">
          取消
        </button>
        <button class="btn" @click="submit">确认</button>
      </div>
    </div>
  </div>
</template>

<script>
// 引入api
import { reviseAddress, addAddress } from "@/utils/Api/SettleApi";
export default {
  props: ["dialogConfig", "form", "idx"],
  data() {
    return {};
  },
  methods: {
    // 取消关闭dialog
    cancel() {
      this.$emit("cancel");
    },
    // 调用获取收货地址接口
    getAddress() {
      this.$emit("getAddress");
    },
    // 清空表单
    clearForm() {
      this.$emit("clearForm");
    },
    // 点击确认
    async submit() {
      if (this.dialogConfig.type == "revise") {
        // 修改地址
        let { data } = await reviseAddress(this.form.id, this.form);
        if (data.msg == "操作成功") {
          this.$message({ text: "修改地址成功", type: "success" });
          this.getAddress();
          this.cancel();
        }
      } else if (this.dialogConfig.type == "change") {
        // 切换地址
        this.$emit("changeIndex", this.idx);
        this.cancel();
      } else if (this.dialogConfig.type == "add") {
        // 添加地址
        let { data } = await addAddress(this.form);
        if (data.msg == "操作成功") {
          this.$message({ text: "添加地址成功", type: "success" });
          this.getAddress();
          this.cancel();
          this.clearForm();
        }
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.btn {
  border-color: #27ba9b;
  background: #27ba9b;
  color: #fff;
  width: 180px;
  height: 50px;
  font-size: 16px;
  outline: none;
  text-align: center;
  border: 1px solid transparent;
  border-radius: 4px;
  text-overflow: ellipsis;
  overflow: hidden;
  cursor: pointer;
}
.xtx-dialog {
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  z-index: 999;
  background: rgba(0, 0, 0, 0);
  &.fade {
    transition: all 0.4s;
    background: rgba(0, 0, 0, 0.5);
  }
  .wrapper {
    width: 600px;
    background: #fff;
    border-radius: 4px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -60%);
    &.fade {
      transition: all 0.4s;
      transform: translate(-50%, -50%);
      opacity: 1;
    }
    .body {
      padding: 20px 40px;
      font-size: 16px;
      .icon-warning {
        color: red;
        margin-right: 3px;
        font-size: 16px;
      }
    }
    .footer {
      text-align: center;
      padding: 10px 0 30px 0;
    }
    .header {
      position: relative;
      height: 70px;
      line-height: 70px;
      padding: 0 20px;
      border-bottom: 1px solid #f5f5f5;
      h3 {
        font-weight: normal;
        font-size: 18px;
      }
      a {
        position: absolute;
        right: 25px;
        top: 25px;
        font-size: 24px;
        width: 20px;
        height: 20px;
        line-height: 20px;
        text-align: center;
        color: #999;
        &:hover {
          color: #666;
        }
      }
    }
  }
}
</style>
